package fuxi2;

import java.util.Random;
import java.util.Scanner;

public class TwoArray {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Random rad=new Random();
        System.out.println("请输入行数和列的数字");
        int n=sc.nextInt();
        int[][] twoa=new int[n][n];
        //二维数组元素用随机数填充
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                twoa[i][j]=rad.nextInt(100)+1;
                System.out.print(twoa[i][j]+" ");
            }
            System.out.println();
        }
        int sum=0;
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
               if(i==j){
                   sum+=twoa[i][j];
               }
            }
        }

        System.out.println("这个二维数组的对角线和为"+sum);


    }
}
